// mycomponent/reference/reference.js
const app = getApp()
import { recommendIdentityEnum } from '../../utils/dataEnum'
Component({
  options: {styleIsolation: 'shared'},
  /**
   * 组件的属性列表
   */
  properties: {
    type: {
      type: Number,
      value: 0
    },
    selectedUserId: {
      type: String || Number,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    keyword: '',
    userList: [],
    recommendIdentityEnum,
    container: null,
    tagColorEnum: {
      1: '#E8F9FF',
      2: '#EEFFE8',
      3: '#FFF7E8'
    },
    tagTextColorEnum: {
      1: '#0078FD',
      2: '#08D564',
      3: '#EEA005'
    },
    // 请求参数对象
    queryObj:{
      page: 1,
      page_size: 10
    },
    // 总数量，用来实现分页
    total: 0,
    // 是否正在请求数据
    isloading: false,
    finished: false,
    refreshing: false
  },

  lifetimes: {
    attached: function() {
      this.search()
      this.setData({
        container: () => wx.createSelectorQuery().select('#reference-wrapper'),
      });
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 当输入框的内容发生变化时，更新数据
     * @param {Object} event 
     */
    onFieldChange(e) {
      this.setData({
        keyword: e.detail
      });
    },

    /**
     * 搜索
     */
    search: function() {
      this.resetQuery()
      this.getData()
    },

    /**
     * 获取数据
     */
    getData: function() {
      const { keyword, type, queryObj, userList } = this.data
      const urlStr = app.getNetAddresss('member.recommend.getList')
      let data = { keyword, search_type: type, offset: userList.length, limit: queryObj.page_size }
      const _this = this
      wx.showLoading({ title: '加载中...' })
      app._getNetWork({
        url: urlStr,
        data,
        success: function(res) {
          const resData = res.data
          if (resData.result == 1) {
            _this.setData({ userList: [...userList, ...resData.data.list]})
            _this.setData({
              finished: _this.data.userList.length >= resData.data.count
            })
          } else {
            app.tips(resData.msg || '数据获取失败！')
          }
        },
        complete: function() {
          wx.hideLoading()
          _this.setData({ refreshing: false })
        }
      })
    },

    /**
     * 关闭
     */
    close: function() {
      this.triggerEvent('close')
    },

    /**
     * 用户选中
     * @param {Object} e 
     */
    seleced: function(e) {
      const { user } = e.currentTarget.dataset
      // 已选择的用户
      if (user.id == this.data.selectedUserId) return
      // const data = {
      //   recommend_id: user.uid,
      //   recommend_name: user.uid_name,
      //   zhang_lv_id: user.id,
      //   zhang_lv_name: user.name,
      //   type: user.type
      // }
      this.triggerEvent('seleceUser', user)
    },

  /**
   * 重置页面请求数据
   */
  resetQuery: function() {
    this.setData({
      'queryObj.page': 1,
      total : 0,
      isloading : false,
      userList : [],
      finished: false
    })
  },

    onPullDownRefresh: function() {
      this.resetQuery()
      this.setData({
        refreshing: true
      })
      this.getData()
    },

    onReachBottom: function() {
      if (this.data.finished) {
        return
      }
      this.getData()
    }
  }
})